package de.bazinga.rotw.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;
import java.util.Vector;

import de.bazinga.rotw.util.GlobalSettings;

public class ServerList {
	
	private static ServerList instance = null;
	private Vector<Hashtable<String,String>> serverList = new Vector<Hashtable<String,String>>();
	
	
	private ServerList() {
		
	}
	
	public static ServerList getInstance() {
		if(instance == null) instance = new ServerList();
		return instance;
	}
	
	
	public Vector<Hashtable<String,String>> getServerList() {
		return serverList;
	}
	
	public void updateServerlist() {
		serverList.clear();
		
		try {
			URL url = new URL(GlobalSettings.SERVERLISTURL+"?page=saveServerListUpdate&do=getServerlist");
			
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String inputLine;
            System.out.println("-----READ SERVERLIST-----");
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                String[] serverData = inputLine.split("--DIVIDER--");
                if(serverData.length == 2) {
                	Hashtable<String,String> data = new Hashtable<String,String>();
                	data.put("ip", serverData[0]);
                	data.put("serverName", serverData[1]);
                	serverList.add(data);
                }
                
            }
            in.close();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}
	
	
	public static void sendServerUpdate(String serverName) {
		try {
			URL url = new URL(GlobalSettings.SERVERLISTURL+"?page=saveServerListUpdate&do=addServer&serverName="+serverName);
			//URL url = new URL("http://localhost/LDCMS3/?page=saveUpdate&username="+username+"&gameCode="+gameCode+"&score="+updateScore+"&time="+seconds);
			//URL url = new URL("http://localhost/rotw/update.php?score="+updateScore+"&time="+seconds);
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String inputLine;
            System.out.println("-----RESPONSE START-----");
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			//e.printStackTrace();
		}
	}
	
	public static void closeServer() {
		try {
			URL url = new URL(GlobalSettings.SERVERLISTURL+"?page=saveServerListUpdate&do=deleteServer");
			//URL url = new URL("http://localhost/LDCMS3/?page=saveUpdate&username="+username+"&gameCode="+gameCode+"&score="+updateScore+"&time="+seconds);
			//URL url = new URL("http://localhost/rotw/update.php?score="+updateScore+"&time="+seconds);
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String inputLine;
            System.out.println("-----RESPONSE START-----");
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			//e.printStackTrace();
		}
	}
}
